package com.navinfo.opentsp.platform.computing.analysis.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.navinfo.opentsp.platform.computing.analysis.util.DataUtil;
import org.apache.spark.sql.Row;
import scala.collection.mutable.WrappedArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VehicleStatusAdditionDto {

    private String types;
    private long statusValue;

    public VehicleStatusAdditionDto() {}

    public VehicleStatusAdditionDto(String types, long statusValue) {
        this.types = types;
        this.statusValue = statusValue;
    }

    public String getTypes() {
        return types;
    }

    public void setTypes(String types) {
        this.types = types;
    }

    public long getStatusValue() {
        return statusValue;
    }

    public void setStatusValue(long statusValue) {
        this.statusValue = statusValue;
    }

    public static VehicleStatusAdditionDto parseString(String obj){
        String[] arr = obj.split("_");
        return new VehicleStatusAdditionDto(arr[0], Long.parseLong(arr[1]));
    }

    public static List<VehicleStatusAdditionDto> parseToList(String statuslist){
        if(statuslist == null){
            return Collections.emptyList();
        }
        TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};
        List<String> dataList = JSON.parseObject(statuslist,typeReference);
        List<VehicleStatusAdditionDto> statusList = new ArrayList<>();
        for(String data : dataList){
            statusList.add(parseString(data));
        }
        return statusList;
    }

    public static List<VehicleStatusAdditionDto> parseRow(Row structRow){
        if(structRow == null){
            return Collections.emptyList();
        }
        List<VehicleStatusAdditionDto> statusList = new ArrayList<>();
        WrappedArray.ofRef array = structRow.getAs("status");
        if(array == null){
            return Collections.emptyList();
        }
        scala.collection.Iterator<Row> itor = array.iterator();
        while(itor.hasNext()){
            Row row = itor.next();
            statusList.add(new VehicleStatusAdditionDto(DataUtil.getValue(row.getAs("types"), 0), DataUtil.getValue(row.getAs("statusvalue"), 0)));
        }
        return statusList;
    }
}
